home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 July: Mac OS SDK / Dev.CD Jul 99 SDK1.toast / Development Kits / Mac OS / OpenGL 1.0 SDK / Source / Libraries / tk / tkevent.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-05-18  |  11.0 KB  |  583 lines  |  [TEXT/CWIE]

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3.  
  4. #include "tk.h"
  5. #include "tkprivate.h"
  6. #include "tkmacglobals.h"
  7.  
  8.  
  9. #if defined(__MWERKS__) || defined(__SC__)
  10.     #include "console.h"
  11. #endif
  12.  
  13. #if defined(__MWERKS__)
  14.     #include "SIOUX.h"
  15. #endif
  16.  
  17. void    (*ExposeFunc)(int, int) = NULL;
  18. void    (*ReshapeFunc)(int, int) = NULL;
  19. void    (*DisplayFunc)(void) = NULL;
  20. GLenum    (*KeyDownFunc)(int, GLenum) = NULL;
  21. GLenum    (*MouseDownFunc)(int, int, GLenum) = NULL;
  22. GLenum    (*MouseUpFunc)(int, int, GLenum) = NULL;
  23. GLenum    (*MouseMoveFunc)(int, int, GLenum) = NULL;
  24. void    (*IdleFunc)(void) = NULL;
  25.  
  26. Boolean    gDone = false;
  27. Boolean    gDrawFlag = true;
  28.  
  29. void DoMenuCommand(long    menuAndItem);
  30. void DoDrag(WindowPtr    myWindow,Point    mouseloc);
  31. void DoContentClick(WindowPtr    myWindow, EventRecord *event);
  32. Boolean IsDAccWindow(WindowPtr    myWindow);
  33. void DoGoAwayBox(WindowPtr myWindow, Point mouseloc);
  34. void DoCloseWindow(WindowPtr myWindow);
  35. void DoKeyDown(EventRecord *event);
  36. void DoDiskEvent(EventRecord *event);
  37. void DoOSEvent(EventRecord *event);
  38. void DoUpdate(WindowPtr myWindow);
  39. void DoZoom(WindowPtr myWindow, Point mouseloc, int part);
  40. void DoActivate(WindowPtr myWindow, int myModifiers);
  41. void DoAboutBox(void);
  42. void DoNextEvent(void);
  43. void DoMenuCommand(long    menuAndItem);
  44. void DoReshape(int width,int height);
  45. void DoCallIdle(void);
  46. void DoMouseDown(EventRecord *event);
  47. void DoMouseUp(EventRecord *event);
  48. void DoMenuAdjust(void);
  49. void DoKeyUp(EventRecord *event);
  50.  
  51. static void DoGrow(WindowPtr myWindow, Point mouseloc)
  52. {
  53.     Rect    growBounds;
  54.     long    newSize;
  55.     
  56.     growBounds = (**GetGrayRgn()).rgnBBox;
  57.     growBounds.right -= growBounds.left;
  58.     growBounds.bottom -= growBounds.top;
  59.     growBounds.left = 20;
  60.     growBounds.top = 20;
  61.     
  62.     newSize = GrowWindow(myWindow,mouseloc,&growBounds);
  63.     
  64.     if(newSize)
  65.     {
  66.         SizeWindow(myWindow, newSize & 0x0000FFFF, newSize >> 16, false);
  67.         SetPort(myWindow);
  68.         DoReshape(LoWord(newSize),HiWord(newSize));
  69.     }
  70. }
  71.  
  72. static void DoZoom(WindowPtr myWindow, Point mouseloc, int part)
  73. {
  74.     short w, h;
  75.     
  76.     if(TrackBox(myWindow, mouseloc, part))
  77.     {
  78.         ZoomWindow(myWindow, part, false);
  79.         w = abs(myWindow->portRect.right - myWindow->portRect.left);
  80.         h = abs(myWindow->portRect.top - myWindow->portRect.bottom);
  81.  
  82.         DoReshape(w, h);
  83.     }
  84. }
  85.  
  86. static void DoReshape(int width, int height)
  87. {
  88.     tkWindow.width = width;
  89.     tkWindow.height = height;
  90.     
  91.     if(!tkWindow.context) return;
  92.     
  93.     aglUpdateContext(aglGetCurrentContext());
  94.  
  95.     if(ReshapeFunc)
  96.     {        
  97.         (*ReshapeFunc)(tkWindow.width,tkWindow.height);
  98.         gDrawFlag = true;
  99.     }
  100. }
  101.  
  102. static void DoRedraw(void)
  103. {
  104.     static GLboolean update_grow = GL_TRUE;
  105.     
  106.     if(!tkWindow.context) return;
  107.     
  108.     if(DisplayFunc && gDrawFlag) 
  109.     {
  110.         gDrawFlag = false;
  111.         (*DisplayFunc)();
  112.     }
  113. }
  114.  
  115. void DoCallIdle(void)
  116. {
  117.     if(!tkWindow.context) return;
  118.     
  119.     if(IdleFunc)
  120.     {
  121.         (*IdleFunc)();
  122.         gDrawFlag = true;
  123.     }
  124. }
  125.  
  126. void DoMouseDown(EventRecord *event)
  127. {    
  128.     int            myPart;
  129.     WindowPtr    myWindow;
  130.     
  131.     myPart = FindWindow(event->where, &myWindow);
  132.     
  133.     switch(myPart)
  134.     {
  135.         case inMenuBar:
  136.             DrawMenuBar();
  137.             DoMenuCommand(MenuSelect(event->where));
  138.         break;
  139.         case inSysWindow:
  140.             SystemClick(event, myWindow);
  141.         break;
  142.         case inDrag:
  143.             DoDrag(myWindow, event->where);
  144.         break;
  145.         case inGoAway:
  146.             DoGoAwayBox(myWindow, event->where);
  147.         break;
  148.         case inGrow:
  149.             DoGrow(myWindow, event->where);
  150.         break;
  151.         case inZoomIn:
  152.         case inZoomOut:
  153.             DoZoom(myWindow, event->where, myPart);
  154.         break;
  155.         case inContent:
  156.             if (myWindow != FrontWindow())
  157.             {
  158.                 SelectWindow(myWindow);
  159.             }
  160.             else
  161.             {
  162.                 DoContentClick(myWindow,event);
  163.             }
  164.         break;
  165.     }
  166. }
  167.  
  168. void DoMouseUp(EventRecord *event)
  169. {
  170.     if(MouseUpFunc)
  171.     {
  172.         if(event->modifiers & shiftKey)
  173.         {
  174.             gDrawFlag = (*MouseUpFunc)(event->where.h, event->where.v, TK_RIGHTBUTTON);
  175.         }
  176.         else if(event->modifiers & optionKey)
  177.         {
  178.             gDrawFlag = (*MouseUpFunc)(event->where.h, event->where.v, TK_MIDDLEBUTTON);
  179.         }
  180.         else
  181.         {
  182.             gDrawFlag = (*MouseUpFunc)(event->where.h, event->where.v, TK_LEFTBUTTON);
  183.         }
  184.     }
  185. }
  186.  
  187. void DoDrag(WindowPtr myWindow, Point mouseloc)
  188. {
  189.     Rect    dragBounds;
  190.     
  191.     dragBounds = (**GetGrayRgn()).rgnBBox;
  192.     DragWindow(myWindow,mouseloc,&dragBounds);
  193.  
  194.     aglUpdateContext(aglGetCurrentContext());
  195. }
  196.  
  197. void DoContentClick(WindowPtr myWindow, EventRecord *event)
  198. {
  199.     if(tkWindow.fullscreen) return;
  200.     
  201.     SetPort(myWindow);
  202.     GlobalToLocal(&event->where);
  203.     
  204.     if(MouseDownFunc)
  205.     {
  206.         if(event->modifiers & shiftKey)
  207.         {
  208.             gDrawFlag = (*MouseDownFunc)(event->where.h, event->where.v, TK_RIGHTBUTTON);
  209.         }
  210.         else if(event->modifiers & optionKey)
  211.         {
  212.             gDrawFlag = (*MouseDownFunc)(event->where.h, event->where.v, TK_MIDDLEBUTTON);
  213.         }
  214.         else
  215.         {
  216.             gDrawFlag = (*MouseDownFunc)(event->where.h, event->where.v, TK_LEFTBUTTON);
  217.         }
  218.     }
  219. }
  220.  
  221. Boolean IsDAccWindow(WindowPtr myWindow)
  222. {
  223.     if(myWindow == nil) 
  224.     { 
  225.         return false;
  226.     }
  227.     else
  228.     { 
  229.         return(((WindowPeek)myWindow)->windowKind < 0);
  230.     }
  231. }
  232.  
  233. void DoGoAwayBox(WindowPtr myWindow, Point mouseloc)
  234. {
  235.     if(TrackGoAway(myWindow,mouseloc))
  236.     { 
  237.         DoCloseWindow(myWindow); 
  238.     }
  239. }
  240.  
  241. void DoCloseWindow(WindowPtr myWindow)
  242. {
  243.     if(myWindow)
  244.     {
  245.         if(IsDAccWindow(myWindow))
  246.         {
  247.             CloseDeskAcc(((WindowPeek)myWindow)->windowKind);
  248.         }
  249.         else 
  250.         {
  251.             tkQuit();
  252.         }
  253.     }
  254. }
  255.  
  256. void DoMenuAdjust(void)
  257. {
  258. }
  259.  
  260. void DoKeyDown(EventRecord *event)
  261.     char    myCharCode;
  262.     char    myKeyCode;
  263.     int        myKey;
  264.     GLenum    mask;
  265.     
  266.     myCharCode    = (char)BitAnd(event->message,charCodeMask);
  267.     
  268.     if(BitAnd(event->modifiers,cmdKey) != 0)
  269.     {
  270.         DoMenuAdjust();
  271.         DoMenuCommand(MenuKey(myCharCode));
  272.     }
  273.     else 
  274.     {
  275.         myKeyCode = event->message & keyCodeMask;
  276.         
  277.         switch(myCharCode)
  278.         {
  279.             case '0':    myKey    = TK_0; break;
  280.             case '1':    myKey    = TK_1; break;
  281.             case '2':    myKey    = TK_2; break;
  282.             case '3':    myKey    = TK_3; break;
  283.             case '4':    myKey    = TK_4; break;
  284.             case '5':    myKey    = TK_5; break;
  285.             case '6':    myKey    = TK_6; break;
  286.             case '7':    myKey    = TK_7; break;
  287.             case '8':    myKey    = TK_8; break;
  288.             case '9':    myKey    = TK_9; break;
  289.             case 'A':    myKey    = TK_A; break;
  290.             case 'B':    myKey    = TK_B; break;
  291.             case 'C':    myKey    = TK_C; break;
  292.             case 'D':    myKey    = TK_D; break;
  293.             case 'E':    myKey    = TK_E; break;
  294.             case 'F':    myKey    = TK_F; break;
  295.             case 'G':    myKey    = TK_G; break;
  296.             case 'H':    myKey    = TK_H; break;
  297.             case 'I':    myKey    = TK_I; break;
  298.             case 'J':    myKey    = TK_J; break;
  299.             case 'K':    myKey    = TK_K; break;
  300.             case 'L':    myKey    = TK_L; break;
  301.             case 'M':    myKey    = TK_M; break;
  302.             case 'N':    myKey    = TK_N; break;
  303.             case 'O':    myKey    = TK_O; break;
  304.             case 'P':    myKey    = TK_P; break;
  305.             case 'Q':    myKey    = TK_Q; break;
  306.             case 'R':    myKey    = TK_R; break;
  307.             case 'S':    myKey    = TK_S; break;
  308.             case 'T':    myKey    = TK_T; break;
  309.             case 'U':    myKey    = TK_U; break;
  310.             case 'V':    myKey    = TK_V; break;
  311.             case 'W':    myKey    = TK_W; break;
  312.             case 'X':    myKey    = TK_X; break;
  313.             case 'Y':    myKey    = TK_Y; break;
  314.             case 'Z':    myKey    = TK_Z; break;
  315.             case 'a':    myKey    = TK_a; break;
  316.             case 'b':    myKey    = TK_b; break;
  317.             case 'c':    myKey    = TK_c; break;
  318.             case 'd':    myKey    = TK_d; break;
  319.             case 'e':    myKey    = TK_e; break;
  320.             case 'f':    myKey    = TK_f; break;
  321.             case 'g':    myKey    = TK_g; break;
  322.             case 'h':    myKey    = TK_h; break;
  323.             case 'i':    myKey    = TK_i; break;
  324.             case 'j':    myKey    = TK_j; break;
  325.             case 'k':    myKey    = TK_k; break;
  326.             case 'l':    myKey    = TK_l; break;
  327.             case 'm':    myKey    = TK_m; break;
  328.             case 'n':    myKey    = TK_n; break;
  329.             case 'o':    myKey    = TK_o; break;
  330.             case 'p':    myKey    = TK_p; break;
  331.             case 'q':    myKey    = TK_q; break;
  332.             case 'r':    myKey    = TK_r; break;
  333.             case 's':    myKey    = TK_s; break;
  334.             case 't':    myKey    = TK_t; break;
  335.             case 'u':    myKey    = TK_u; break;
  336.             case 'v':    myKey    = TK_v; break;
  337.             case 'w':    myKey    = TK_w; break;
  338.             case 'x':    myKey    = TK_x; break;
  339.             case 'y':    myKey    = TK_y; break;
  340.             case 'z':    myKey    = TK_z; break;
  341.             case ' ':    myKey    = TK_SPACE; break;
  342.             case 0x0D:    myKey    = TK_RETURN; break;
  343.             case 0x1B:    myKey    = TK_ESCAPE; break;
  344.             case 0x1C:    myKey    = TK_LEFT; break;
  345.             case 0x1D:    myKey    = TK_RIGHT; break;
  346.             case 0x1E:    myKey    = TK_UP; break;
  347.             case 0x1F:    myKey    = TK_DOWN; break;
  348.             default:     myKey    = GL_FALSE; break; 
  349.         }
  350.         
  351.         if (myKey && KeyDownFunc)
  352.         {
  353.             mask = 0;
  354.             
  355.             if (event->modifiers & controlKey)
  356.             {
  357.                 mask |= TK_CONTROL;
  358.             }
  359.             
  360.             if (event->modifiers & shiftKey)
  361.             {
  362.                 mask |= TK_SHIFT;
  363.             }
  364.             
  365.             gDrawFlag = true;
  366.             (*KeyDownFunc)(myKey, mask);
  367.             
  368.             return;
  369.         }
  370.     }
  371. }
  372.  
  373. static void DoDiskEvent(EventRecord    *event)
  374. {
  375.  
  376. }
  377.  
  378. static void    DoOSEvent(EventRecord    *event)
  379. {
  380.  
  381. }
  382.  
  383. static void DoUpdate(WindowPtr    myWindow)
  384.     GrafPtr        origPort;
  385.     
  386.     GetPort(&origPort);
  387.     SetPort(myWindow);
  388.         
  389.     BeginUpdate(myWindow);    
  390.     EndUpdate(myWindow);
  391.     
  392.     aglUpdateContext(aglGetCurrentContext());
  393.     
  394.     SetPort(origPort);
  395.         
  396.     gDrawFlag = true;
  397. }
  398.  
  399. static void DoActivate(WindowPtr    myWindow,int    myModifiers)
  400. {
  401.  
  402. }
  403.  
  404. static void DoAboutBox(void)
  405. {
  406.     DialogPtr    myDialog;
  407.     short        itemHit;
  408.  
  409.     myDialog = GetNewDialog(kAboutDialog, nil, (WindowPtr) -1);
  410.     ModalDialog(nil, &itemHit);
  411.     DisposeDialog(myDialog);
  412. }
  413.  
  414. static void DoMenuCommand(long    menuAndItem)
  415. {
  416.     int            myMenuNum;
  417.     int            myItemNum;
  418.     int            myResult;
  419.     Str255        myDAName;
  420.     WindowPtr    myWindow;
  421.     
  422.     myMenuNum    = HiWord(menuAndItem);
  423.     myItemNum    = LoWord(menuAndItem);
  424.     
  425.     GetPort(&myWindow);
  426.     
  427.     switch (myMenuNum) 
  428.     {
  429.         case mApple:
  430.             switch (myItemNum)
  431.             {
  432.                 case iAbout: 
  433.                     DoAboutBox();
  434.                 break;
  435.                 default: 
  436.                     GetMenuItemText(GetMenuHandle(mApple), myItemNum, myDAName);
  437.                     myResult = OpenDeskAcc(myDAName);
  438.                 break;
  439.             }
  440.         break;
  441.         case mFile:
  442.             switch (myItemNum)
  443.             {
  444.                 case iQuit:
  445.                     tkQuit();
  446.                 break;
  447.             }
  448.         break;
  449.     }
  450.     
  451.     HiliteMenu(0);
  452. }
  453.  
  454. static Boolean DoConsoleEvent(EventRecord *event)
  455. {
  456.     GLboolean flag = GL_FALSE;
  457.     
  458.     #if defined(__MWERKS__)
  459.         flag = SIOUXHandleOneEvent(event);
  460.     #endif
  461.     
  462.     return flag;
  463. }
  464.  
  465. static void DoNextEvent(void)
  466. {
  467.     Boolean           gotEvent;
  468.     EventRecord       event;
  469.     static double  t0 = -10.0, t1;
  470.     
  471.     gotEvent = WaitNextEvent(everyEvent, &event, 0, nil);
  472.  
  473.     if(gotEvent && !DoConsoleEvent(&event)) 
  474.     {
  475.         switch(event.what)
  476.         {
  477.             case mouseDown:
  478.                 DoMouseDown(&event);
  479.             break;
  480.             case mouseUp:
  481.                 DoMouseUp(&event);
  482.             break;
  483.             case keyDown:
  484.                 DoKeyDown(&event);
  485.             break;
  486.             case keyUp:
  487.             break;
  488.             case autoKey:
  489.                 DoKeyDown(&event);
  490.             break;
  491.             case updateEvt:
  492.                 DoUpdate((WindowPtr) event.message);
  493.             break;
  494.             case diskEvt:
  495.                 DoDiskEvent(&event);
  496.             break;
  497.             case activateEvt:
  498.                 DoActivate((WindowPtr) event.message, event.modifiers);
  499.             break;
  500.             case osEvt:
  501.                 DoOSEvent(&event);
  502.             break;
  503.             default:
  504.                 DoCallIdle();
  505.             break;
  506.         }
  507.     }
  508.     else
  509.     {
  510.         DoCallIdle();
  511.     }
  512. }
  513.  
  514. void tkExec(void)
  515. {
  516.     gDrawFlag = true;
  517.  
  518.     if(tkWindow.drawable)
  519.     {
  520.         BeginUpdate((GrafPort *) tkWindow.drawable);
  521.         EndUpdate((GrafPort *) tkWindow.drawable);
  522.     }
  523.     
  524.     DoReshape(tkWindow.width,tkWindow.height);
  525.  
  526.     while(!gDone)
  527.     {
  528.         DoNextEvent();
  529.         DoRedraw();
  530.     }
  531.     
  532.     tkCloseWindow();
  533. }
  534.  
  535.  
  536. void tkExposeFunc(void (*Func)(int, int))
  537. {
  538.     ExposeFunc = Func;
  539. }
  540.  
  541.  
  542. void tkReshapeFunc(void (*Func)(int, int))
  543. {
  544.     ReshapeFunc = Func;
  545. }
  546.  
  547.  
  548. void tkDisplayFunc(void (*Func)(void))
  549. {
  550.     DisplayFunc = Func;
  551. }
  552.  
  553.  
  554. void tkKeyDownFunc(GLenum (*Func)(int, GLenum))
  555. {
  556.     KeyDownFunc = Func;
  557. }
  558.  
  559. void tkMouseDownFunc(GLenum (*Func)(int, int, GLenum))
  560. {
  561.     MouseDownFunc = Func;
  562. }
  563.  
  564.  
  565. void tkMouseUpFunc(GLenum (*Func)(int, int, GLenum))
  566. {
  567.     MouseUpFunc = Func;
  568. }
  569.  
  570.  
  571. void tkMouseMoveFunc(GLenum (*Func)(int, int, GLenum))
  572. {
  573.     MouseMoveFunc = Func;
  574. }
  575.  
  576.  
  577. void tkIdleFunc(void (*Func)(void))
  578. {
  579.     IdleFunc = Func;
  580. }
  581.